// Copyright (C) 2016  Francisco Javier Parra <franparpe@openmailbox.org>

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#include "xmpplar_client.h"

XmpplarClient::XmpplarClient(MainWindow *caller, gloox::JID &jid,
                             std::string password)
  : gloox::Client(jid, password),
  m_caller(caller)
{
  registerMessageHandler(this);
  registerConnectionListener(this);
}

bool XmpplarClient::startConnection()
{
  return connect();
}

void XmpplarClient::onConnect()
{
  std::cout << "connection established" << "\n";
}

void XmpplarClient::onDisconnect( gloox::ConnectionError e )
{
  if (e) {
    std::cout << streamErrorText() << "\n";
  }
}

bool XmpplarClient::onTLSConnect( const gloox::CertInfo &info )
{
  return true;
}

void XmpplarClient::handleMessage(const gloox::Message &stanza,
                   gloox::MessageSession* session)
{
  //Continue here
  m_message_cnt = stanza.body();
  m_caller->message_notification();
}

std::string XmpplarClient::getBody()
{
  return m_message_cnt;
}
